import 'dart:io';

import 'package:flutter/material.dart';
import 'main.dart';
import 'package:flutter/services.dart';
import 'net.dart' as net;
import 'package:image_picker/image_picker.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'dart:convert' as convert;
import 'bean/beans.dart';

const METHOD_CHANNEL = const MethodChannel("dar.flutter.io/method_channel");
const EVENT_CHANNEL = const EventChannel("dar.flutter.io/event_channel");

//------------------------------------------test
class TestWidget extends StatefulWidget {
  TestWidget();

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return TestState();
  }
}

class TestState extends State<TestWidget> {
  String _batteryLevel = 'Unknown battery level.';
  File _image;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(),
      body: Column(
        children: <Widget>[
          FlatButton.icon(
              onPressed: () {
//                _getBatteryLevel();
                channelGetChapters(1, 20);
//                channelUpdateBookExtra();
              },
              icon: Icon(Icons.account_balance_wallet),
              label: Text("test")),
          Text(_batteryLevel),
          FlatButton.icon(
              onPressed: () {
                _getImage();
              },
              icon: Icon(Icons.accessibility),
              label: Text("select")),
          FlatButton.icon(
              onPressed: () {
                net.uploadHeadImg(_image, "123456.jpg");
              },
              icon: Icon(Icons.accessibility),
              label: Text("upload")),
          Container(
            child: _image == null ? null : Image.file(_image),
          ),
          CachedNetworkImage(
//            imageUrl: "http://bmob-cdn-23479.b0.upaiyun.com/2019/06/05/c5b72f754035870280eb4db6f176130a.jpg",
            imageUrl: "http://pic37.nipic.com/20140113/8800276_184927469000_2.png",
            fit: BoxFit.cover,
            placeholder: (context, url) {
              return Container(
                color: Color(0xffefeff4),
              );
            },
          )

        ],
      ),
    );
  }

  Future<Null> _getBatteryLevel() async {
    String batteryLevel;
    try {
      final int result = await METHOD_CHANNEL.invokeMethod('getBatteryLevel');
      batteryLevel = 'Battery level at $result % .';
    } on PlatformException catch (e) {
      batteryLevel = "Failed to get battery level: '${e.message}'.";
    }

    setState(() {
      _batteryLevel = batteryLevel;
    });
  }

  Future _getImage() async {
    var image = await ImagePicker.pickImage(source: ImageSource.gallery);

    setState(() {
      _image = image;
    });
  }

  Future channelGetChapters(int page, int pageSize) async {
    try {
      final String result = await METHOD_CHANNEL.invokeMethod(
          'getChaptersJsonInfo', {"platformbid": "9409545", "platform": "xmly", "page": page, "pageSize": pageSize});
      print("channelGetChapters result = $result");
      List list = convert.jsonDecode(result);
      if (list != null) {
        print("channelGetChapters length: ${list.length}");
        for (var l in list) {
          Chapter chapter = Chapter.fromXmlyJson(l);
        }
      }
    } catch (e) {
      print("channelGetChapters exception: $e");
    }
    return null;
  }

  Future<bool> channelUpdateBookExtra() async {
    try {
      final String result =
          await METHOD_CHANNEL.invokeMethod('getAlbumJsonInfo', {"platformbid": "9409545", "platform": "xmly"});
      print("channelUpdateBookExtra result = $result");
      Book().updateFromPlatformJson(convert.jsonDecode(result));
      return true;
    } catch (e) {
      print("channelUpdateBookExtra exception: $e");
    }
    return false;
  }
}
